/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.ejb.facade.user;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.ipo.mm.ejb.entities.User;

/**
 *
 * @author makcro
 */
@Stateless
public class UserFacade implements UserFacadeLocal {

    @PersistenceContext
    private EntityManager em;

    public void create(User user) {
        em.persist(user);
    }

    public void edit(User user) {
        em.merge(user);
    }

    public void remove(User user) {
        em.remove(em.merge(user));
    }

    public User find(Object id) {
        return em.find(User.class, id);
    }

    public List<User> findAll() {
        return em.createNamedQuery("User.findAll").getResultList();
    }

    public Boolean validate(String username, String password) {
        User user = find(username);

        if(user != null){
            if(user.getPassword().equals(password) && user.getDeleted()==false){
                return true;
            }
            else {
                return false;
            }

        }
        else {
            return new Boolean(false);
        }
    }
}
